package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.biz.TransOutboundInvoker;
import com.mall.order.biz.context.AbsTransHandlerContext;
import com.mall.order.biz.factory.OrderProcessPipelineFactory;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 *  cskaoyan
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderDetailsResponse getOrderDetail(OrderDetailsRequest request) {
        OrderDetailsResponse response=new OrderDetailsResponse();
        try{
            request.requestCheck();
            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(request.getOrderId());
            response.setGoodsList(orderConverter.item2dto(orderItems));
            Order order = orderMapper.selectByPrimaryKey(request.getOrderId());
            Double orderTotal = 0.0;
            for (OrderItem orderItem : orderItems) {
                orderTotal += orderItem.getTotalFee();
            }
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(request.getOrderId());

            response.setOrderTotal(orderTotal);
            response.setStreetName(orderShipping.getReceiverAddress());
            response.setTel(orderShipping.getReceiverPhone());
            response.setUserId(order.getUserId());
            response.setUserName(order.getBuyerNick());
            response.setOrderStatus(order.getStatus());

            //封装状态码
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());

        }catch (Exception e){
            response.setCode(OrderRetCode.DB_EXCEPTION.getCode());
            response.setMsg(OrderRetCode.DB_EXCEPTION.getMessage());
            log.error("OrderQueryServiceImpl.orderDetail occur Exception :"+e);
        }
        return response;
    }
}
